package robot;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JToggleButton;


public class ControlPanel {

	private JPanel jPanel;
	private MainWindow mainWindow;
	private RobotConsole robotConsole;
	private JToggleButton startButton;
	private JButton openMap;
	private JButton saveMap;

	public ControlPanel(RobotConsole robotConsole, MainWindow mainWindow) {
		this.robotConsole = robotConsole;
		this.mainWindow = mainWindow;

		jPanel = new JPanel();
		jPanel.setLayout(null);
		jPanel.setPreferredSize(new Dimension(490, 1000));

		Component tmp;

		tmp = addStartButton();
		tmp.setBounds(10, 10, 150, 25);
		jPanel.add(tmp);

		tmp = addOpenMapButton();
		tmp.setBounds(170, 10, 150, 25);
		jPanel.add(tmp);

		tmp = addSaveMapButton();
		tmp.setBounds(330, 10, 150, 25);
		jPanel.add(tmp);
	}

	private Component addStartButton() {
		startButton = new JToggleButton("Start");
		startButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (mainWindow.isWorking()) {
					startButton.setText("Start");
					robotConsole.addText("Przerywam pracę");
					openMap.setEnabled(true);
					saveMap.setEnabled(true);
					mainWindow.setWorking(false);
				}
				else {
					startButton.setText("Stop");
					robotConsole.addText("Zaczynam pracę");
					openMap.setEnabled(false);
					saveMap.setEnabled(false);
					mainWindow.setWorking(true);
				}
			}

			@Override
			public void mousePressed(MouseEvent arg0) {}

			@Override
			public void mouseExited(MouseEvent arg0) {}

			@Override
			public void mouseEntered(MouseEvent arg0) {}

			@Override
			public void mouseClicked(MouseEvent arg0) {}
		});
		return startButton;
	}

	private Component addOpenMapButton() {
		openMap = new JButton("Wczytaj mapę");
		openMap.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (openMap.isEnabled()) {
					robotConsole.addText("Czekam na wybór pliku...");
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setDialogTitle("Wybierz plik");
					fileChooser.showOpenDialog(mainWindow.getComponent());
					File file = fileChooser.getSelectedFile();
					if (file != null) {
						try {
							Path path = Paths.get(file.toURI());
						    byte[] data = Files.readAllBytes(path);
						    if (data.length == 1000000) {
						    	byte[][] map = new byte[1000][1000];
						    	for (int i=0; i<1000000; i++) {
						    		map[i/1000][i%1000] = data[i];
						    	}
						    	mainWindow.setNewMap(map);
						    	robotConsole.addText("Wczytano mapę");
						    }
						    else {
						    	robotConsole.addText("Rozmiar pliku = " +
						    Integer.toString(data.length) + ", musi być 1000000!");
						    }
						} catch (IOException e) {
							e.printStackTrace();
						}
						robotConsole.addText("Wybrano plik: " +
								"\"" + file.getName() + "\"");
					}
					else {
						robotConsole.addText("Zrezygnowano z wyboru mapy");
					}
				}
			}

			@Override
			public void mousePressed(MouseEvent arg0) {}

			@Override
			public void mouseExited(MouseEvent arg0) {}

			@Override
			public void mouseEntered(MouseEvent arg0) {}

			@Override
			public void mouseClicked(MouseEvent arg0) {}
		});
		return openMap;
	}

	private Component addSaveMapButton() {
		saveMap = new JButton("Zapisz mapę");
		saveMap.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (openMap.isEnabled()) {
					robotConsole.addText("Czekam na wybór pliku");
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setDialogTitle("Wybierz plik");
					fileChooser.showSaveDialog(mainWindow.getComponent());
					File file = fileChooser.getSelectedFile();
					if (file != null) {
						try {
							FileWriter fstream = new FileWriter(file);
							byte[] data = new byte[1000*1000];
							byte[][] map = mainWindow.getMap();
							for (int i = 0 ; i < 1000 ; i++) {
								for (int j = 0 ; j < 1000 ; j++) {
									data[i*1000 + j] = map[i][j];
								}
							}
							BufferedWriter out = new BufferedWriter(fstream);
							out.write(new String(data));
							out.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
						robotConsole.addText("Zapisano plik: " +
								"\"" + file.getName() + "\"");
					}
					else {
						robotConsole.addText("Zrezygnowano z zapisu mapy");
					}
				}
			}

			@Override
			public void mousePressed(MouseEvent arg0) {}

			@Override
			public void mouseExited(MouseEvent arg0) {}

			@Override
			public void mouseEntered(MouseEvent arg0) {}

			@Override
			public void mouseClicked(MouseEvent arg0) {}
		});
		return saveMap;
	}

	public JPanel getJPanel() {
		return jPanel;
	}

}
